package com.atguigu.gmall.product.config;

import com.atguigu.gmall.common.config.ItemServiceRedissonConfig;
import lombok.Data;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author dyq
 * @description
 * @data 26/1/2022
 */
@Import(ItemServiceRedissonConfig.class)
@AutoConfigureAfter(RedisAutoConfiguration.class)
@Configuration
public class ProductConfig {

    @Bean("corePool")
    public ThreadPoolExecutor executor(ThreadConfigProperties properties){
        return new ThreadPoolExecutor(
                properties.core,
                properties.max,
                properties.keepAlive,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(properties.queueLength),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
    }

    @Data
    @Component
    @ConfigurationProperties(prefix = "search-service.thread")
    class ThreadConfigProperties{
        private Integer core;
        private Integer max;
        private Long keepAlive;
        private Integer queueLength;
    }
}
